<?php
defined('IN_TS') or die('Access Denied.');
//小组成员

switch($ts){
	
	//小组成员首页
	case "":
	
		$groupid = intval($_GET['id']);
		$usertype = intval($_GET['usertype']);
		//判断是否存在这个群组
		$strGroup = $new['group']->getOneGroup($groupid);
		if($strGroup == '') {
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			$title = '404';
			include pubTemplate("404");
			exit;
		}

		//小组组长信息
		$leaderId = $strGroup['userid'];

		$strLeader = aac('user')->getOneUser($leaderId);

		//管理员信息
		
		$strAdmin = $new['group']->findAll('group_user',array(
			'groupid'=>$strGroup['groupid'],
			'isadmin'=>'1',
			'isfounder'=>'0',
		));
		

		if(is_array($strAdmin)){
			foreach($strAdmin as $key=>$item){
				$arrAdmin[] = aac('user')->getOneUser($item['userid']);
				$arrAdmin[$key]['isadmin'] = $item['isadmin'];
			}
		}

		//小组会员分页

		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

		$url = tsUrl('group','user',array('id'=>$groupid,'usertype'=>$usertype,'page'=>''));


		$lstart = $page*40-40;


		$arr = 'groupid='.$groupid;

		switch($usertype){
			case 1:
				if ($strGroup['isplace'] == 2) {
					$arr = $arr . ' and isvo=1';
				}
				$groupUsers = $new['group']->findAll('group_user',$arr,'addtime desc');
				$groupUserNum = 0;
				foreach($groupUsers as $k=>$v){
					$strUser = aac('user')->getOneUser($v['userid']);
					$arrDuty = aac('duty')->find('duty','groupid='.$groupid.' and userid='.$v['userid'].' and addtime>'.strtotime('-30 day'));
					if($arrDuty){
						$strUsers[] = $v;
						$groupUserNum = $groupUserNum + 1;
					}
				}
				$groupUser = array_slice($strUsers,$lstart,40);
				break;
			case 2:
				$arr=$arr.' and (isstock=1 or isstock=2)';
				$groupUserNum = $new['group']->findCount('group_user',$arr);
				$groupUser = $new['group']->findAll('group_user',$arr,'addtime desc',null,$lstart.',40');
				break;
			case 3:
				$arr = $arr . ' and (isstock=-1)';
				$groupUserNum = $new['group']->findCount('group_user',$arr);
				$groupUser = $new['group']->findAll('group_user',$arr,'addtime desc',null,$lstart.',40');
				break;
			case 4:
				$arrUser = $new['group']->findAll('group_user','groupid='.$groupid.' and isvo=1');
				$groupUserNum = 0;
				$groupUser = array();
				if(is_array($arrUser)){
					foreach($arrUser as $key=> $item){
						$strDuty = aac('duty')->find('duty','groupid='.$groupid.' and userid='.$item['userid'].' and addtime>'.strtotime('-60 day'));
						$strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
						if($strUser['iscoach']==1&&$strDuty){
							$groupUser[] =$item;
							$groupUserNum=$groupUserNum+1;
						}
					}
				}
				break;
			default:
				if ($strGroup['isplace'] == 2) {
					$arr = $arr . ' and isvo=1';
				}
				$groupUserNum = $new['group']->findCount('group_user',$arr);
				$groupUser = $new['group']->findAll('group_user',$arr,'addtime desc',null,$lstart.',40');

		}


		if(is_array($groupUser)){
			foreach($groupUser as $key=>$item){
				$arrGroupUser[] = aac('user')->getOneUser($item['userid']);
				$arrGroupUser[$key]['isadmin'] = $item['isadmin'];
				$arrGroupUser[$key]['isstock'] = $item['isstock'];
			}
		}

		$pageUrl = pagination($groupUserNum, 40, $page, $url);

		if($page > '1'){
			$titlepage = " - 第".$page."页";
		}else{
			$titlepage='';
		}

		$title = $strGroup['groupname'].'成员'.$titlepage;

		include template("user");
		
		break;
		
	//设为管理员 
	case "manager":
		
		$nuserid = intval($TS_USER['userid']);

		if($nuserid==0){
			echo '0';exit;//非法操作
		}
		
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		
		$strGroup = $new['group']->find('group',array(
			'groupid'=>$groupid,
		));
		

			$strGroupUser = $new['group']->find('group_user',array(
				'userid'=>$userid,
				'groupid'=>$groupid,
			));
			
			if($strGroup['userid'] != $userid && ($strGroup['userid']==$nuserid ||$TS_USER['isadmin']==1)){
			
			
			if($strGroupUser['isadmin']==1){

				$new['group']->update('group_user',array(
					'userid'=>$userid,
					'groupid'=>$groupid,
				),array(

					'isadmin'=>0,

				));

			}elseif($strGroupUser['isadmin']==0){

				$new['group']->update('group_user',array(
					'userid'=>$userid,
					'groupid'=>$groupid,
				),array(

					'isadmin'=>1,

				));

			}

			echo '1';exit;


		}else{

			echo '0';exit;

		}
			
			
			

		break;
	

	//设为馆东
	case "stock":

		$nuserid = intval($TS_USER['userid']);

		if($nuserid==0){
			echo '0';exit;//非法操作
		}

		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);

		$strGroup = $new['group']->find('group',array(
			'groupid'=>$groupid,
		));


		$strGroupUser = $new['group']->find('group_user',array(
			'userid'=>$userid,
			'groupid'=>$groupid,
		));


		if($strGroup['userid']==$nuserid ||$TS_USER['isadmin']==1){
			if($strGroupUser['isstock']==1){
				$new['group']->update('group_user',array(
					'userid'=>$userid,
					'groupid'=>$groupid,
				),array(
					'isstock'=>-1,'uptime'=>time()
				));


				$strStock = $new['group']->findall('group_user',array('userid'=>$userid,'isstock'=>1));
				if(!$strStock){
					aac('user')->update('user_info',array('userid'=>$userid),array('isstock'=>-1));

				}



			}else{

				$new['group']->update('group_user',array(
					'userid'=>$userid,
					'groupid'=>$groupid,
				),array(
					'isstock'=>1,'uptime'=>time()
				));
				aac('user')->update('user_info',array('userid'=>$userid),array('isstock'=>1));
			}
			$count_sto=$new['group']->findCount('group_user','(isstock=1 or isstock=2) and groupid='.$groupid);
			$new['group']->update('group',array(
				'groupid'=>$groupid
			),array(
					'count_sto'=>$count_sto
				)
			);
			$groupids = getGroupIds($userid);
			aac('user')->update('user_info',array('userid'=>$userid),array('groupids'=>$groupids));
			echo '1';exit;
		}else{

			echo '0';exit;

		}
		break;

	//退出馆东
	case "quitstock":

		$userid = intval($_POST['userid']);
		if($userid == ''){
			$userid = intval($TS_USER['userid']);
		}

		if($userid==0){
			echo '0';exit;//非法操作
		}

		$groupid = intval($_POST['groupid']);

		$strStock = $new['group']->findall('group_user',array('userid'=>$userid,'isstock'=>1));
		if(!$strStock){
			aac('user')->update('user_info',array('userid'=>$userid),array('isstock'=>-2));
		}

		$groupuser = $new['group']->find('group_user',array('userid'=>$userid,'groupid'=>$groupid));
		if($groupuser['isvo']==1){
			$new['group']->update('group_user',array(
				'userid'=>$userid,
				'groupid'=>$groupid,
			),array(
				'isstock'=>0,
			));
		}else{
			$new['group']->delete('group_user',array('userid'=>$userid,'groupid'=>$groupid));
			$groupids = getGroupIds($userid);
			aac('user')->update('user_info',array('userid'=>$userid),array('groupids'=>$groupids));

			//更新
			$count_group = aac('group')->findCount('group_user',array(
				'userid'=>$userid,
			));
			aac('user')->update('user_info',array('userid'=>$userid),array('count_group'=>$count_group));

			//计算小组会员数
			$count_user = $new['group']->findCount('group_user',array(
				'groupid'=>$groupid,
			));

			//更新小组成员统计
			$new['group']->update('group',array(
				'groupid'=>$groupid,
			),array(
				'count_user'=>$count_user,
			));
		}
		$count_sto=$new['group']->findCount('group_user','(isstock=1 or isstock=2) and groupid='.$groupid);
		$new['group']->update('group',array(
			'groupid'=>$groupid
		),array(
				'count_sto'=>$count_sto
			)
		);
		echo '1';exit;

		break;

	//认定月捐
	case "donate":
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('donate'=>1,'supporter'=>$TS_USER['userid'],'uptime'=>time()));

		echo '1';exit;

		break;

	//认定没月捐
	case "nodonate":
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('donate'=>0,'supporter'=>$TS_USER['userid'],'uptime'=>time()));

		echo '1';exit;

		break;

	case "save":
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		$type = intval($_POST['type']);
		switch($type){
			case 1:
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('supporttype'=>1,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
				aac('user')->create('user_comment',array('userid'=>$userid,'content'=>'申请加微信了','authorid'=>$TS_USER['userid'],'addtime'=>time(),'uptime'=>time(),'type'=>1));
				break;
			case 2:
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('supporttype'=>2,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
				aac('user')->create('user_comment',array('userid'=>$userid,'content'=>'微信留言了','authorid'=>$TS_USER['userid'],'addtime'=>time(),'uptime'=>time(),'type'=>1));
				break;
			case 3:
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('supporttype'=>3,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
				aac('user')->create('user_comment',array('userid'=>$userid,'content'=>'手机搜不到微信','authorid'=>$TS_USER['userid'],'addtime'=>time(),'uptime'=>time(),'type'=>1));
				break;
			case 4:
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('supporttype'=>4,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
				aac('user')->create('user_comment',array('userid'=>$userid,'content'=>'打电话了','authorid'=>$TS_USER['userid'],'addtime'=>time(),'uptime'=>time(),'type'=>1));
				break;
			default:
				aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('wechat'=>1,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
				break;

		}

		echo '1';exit;

		break;




	//认定入群
	case "wechat":
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('wechat'=>1,'supporter'=>$TS_USER['userid'],'uptime'=>time()));
		echo '1';exit;

		break;

	//认定未入群
	case "nowechat":
		$js = intval($_GET['js']);
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$groupid = intval($_POST['groupid']);
		$userid= intval($_POST['userid']);
		aac('group')->update('group_user',array('userid'=>$userid,'groupid'=>$groupid),array('wechat'=>0,'supporter'=>$TS_USER['userid'],'uptime'=>time()));

		echo '1';exit;

		break;


}
